
#include "stdafx.h"
#include "CopyDataResponse.h"
#include "UserMessage.h"

CCopyDataResponse::CCopyDataResponse()
{
	recs = nullptr; count = 0;
}

CCopyDataResponse::~CCopyDataResponse()
{
	if (recs) HeapFree(GetProcessHeap(), 0, recs);
}

void CCopyDataResponse::clear(UINT msg, HWND sender)
{
	ASSERT_MainThread();
	stCopyDataResponseNode *pNode = find(msg, sender);
	if (pNode) pNode->result = COPYDATA_RESPONSE_NOT_ARRIVED;
}

void CCopyDataResponse::set(UINT msg, HWND sender, LRESULT result)
{
	ASSERT_MainThread();
	stCopyDataResponseNode *pNode = find(msg, sender);
	if (pNode) pNode->result = result;
}

LRESULT CCopyDataResponse::get(UINT msg, HWND sender)
{
	ASSERT_MainThread();
	stCopyDataResponseNode *pNode = find(msg, sender);
	return pNode ? pNode->result : COPYDATA_RESPONSE_NOT_ARRIVED;
}

stCopyDataResponseNode *CCopyDataResponse::find(UINT msg, HWND sender)
{
	ASSERT_MainThread();
	if (!sender) return nullptr;
	for (intptr_t i = 0; i < count; i++) if (recs[i].msg == msg && recs[i].sender == sender) return recs + i;
	if ((count & 0xff) == 0)
	{
		stCopyDataResponseNode*tmp = (stCopyDataResponseNode*)(recs ?
			HeapReAlloc(GetProcessHeap(), 0, recs, (count + 256) * sizeof(stCopyDataResponseNode)) :
			HeapAlloc(GetProcessHeap(), 0, 256 * sizeof(stCopyDataResponseNode)));
		if (!tmp) return nullptr;
		recs = tmp;
	}
	stCopyDataResponseNode *pNode = recs + count; count++;
	pNode->msg = msg; pNode->sender = sender; pNode->result = COPYDATA_RESPONSE_NOT_ARRIVED;
	return pNode;
}
